package com.hapzxb.provider.service;

import com.hapzxb.provider.bean.Department;
import com.hapzxb.provider.respository.DepartmentRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRespository respository;

    @Override
    public boolean saveDepartment(Department department) {
        Department result = respository.save(department);
        if (result != null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean removeDepartment(int id) {
        // 对于deleteById()方法，若DB中不存在该id,而执行了其删除操作，将抛出异常
        if (respository.existsById(id)) {
            respository.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public boolean modifyDepart(Department department) {
        Department result = respository.save(department);
        if (result != null) {
            return true;
        }
        return false;
    }

    @Override
    public Department getDepartmentById(int id) {
        // getOne()方法：如其指定的id不存在，该方法将抛出异常
        if (respository.existsById(id)) {
            return respository.getOne(id);
        }
        Department department = new Department();
        department.setName("no this department");
        return department;
    }

    @Override
    public List<Department> listAllDepartment() {
        return respository.findAll();
    }
}
